package com.arcsoft.commonbase.ext

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import com.arcsoft.commonbase.R
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target.SIZE_ORIGINAL
import com.bumptech.glide.request.transition.Transition
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import java.io.File

fun ImageView.loadImage(url: String?, imageOptions: ImageOptions? = null) {
    Glide.with(this.context)
        .load(url)
        .apply(requestOptions(imageOptions))
        .into(this)
}

fun ImageView.loadImageCircle(url: String?) {
    this.loadImage(url, ImageOptions().apply {
        circleCrop = true
        placeholder = R.mipmap.head
        error = R.mipmap.head
        fallback = R.mipmap.head
    })
}

fun ImageView.loadImageCircleCar(url: String?) {
    this.loadImage(url, ImageOptions().apply {
        circleCrop = true
        placeholder = R.mipmap.def_dog
        error = R.mipmap.def_dog
        fallback = R.mipmap.def_dog
    })
}
fun ImageView.loadImage(file: File, imageOptions: ImageOptions? = null) {
    Glide.with(this.context)
        .load(file)
        .apply(requestOptions(imageOptions))
        .into(this)
}
fun ImageView.loadImage(url: String?, overrideWidth: Int, overrideHeight: Int) {
    Glide.with(this.context)
        .load(url)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .override(overrideWidth, overrideHeight)
        .skipMemoryCache(false)
        .priority(Priority.HIGH)
        .into(this)

}
@SuppressLint("CheckResult")
private fun requestOptions(imageOptions: ImageOptions?) = RequestOptions().apply {
    imageOptions?.let {
        transform(MultiTransformation(CenterCrop(), RoundedCornersTransformation(it.cornersRadius, 0)))
        placeholder(it.placeholder)
        error(it.error)
        fallback(it.fallback)
        if (it.circleCrop) {
            circleCrop()
        }
        override(SIZE_ORIGINAL, SIZE_ORIGINAL)
    }
}

class ImageOptions {
    var placeholder = 0         // 加载占位图
    var error = 0               // 错误占位图
    var fallback = 0            // null占位图
    var cornersRadius = 0       // 圆角半径
    var circleCrop = false      // 是否裁剪为圆形
}


fun glidePauseRequests(context: Context) {
    Glide.with(context).pauseRequests()
}

fun glideResumeRequests(context: Context) {
    Glide.with(context).resumeRequests()
}


fun cropEngineLoad(
    context: Context,
    url: Uri,
    maxWidth: Int,
    maxHeight: Int,
    onResourceReady: (resource: Bitmap) -> Unit,
    onLoadCleared: () -> Unit
) {
    Glide.with(context).asBitmap().load(url).override(maxWidth, maxHeight)
        .into(object : CustomTarget<Bitmap?>() {
            override fun onResourceReady(
                resource: Bitmap,
                transition: Transition<in Bitmap?>?
            ) {
                onResourceReady.invoke(resource)
            }

            override fun onLoadCleared(@androidx.annotation.Nullable placeholder: Drawable?) {
                onLoadCleared.invoke()
            }
        })
}